package com.macro.mall.portal.service.impl;

import com.github.pagehelper.PageHelper;
import com.macro.mall.common.api.CommonResult;
import com.macro.mall.common.api.ResultCode;
import com.macro.mall.common.exception.Asserts;
import com.macro.mall.common.utils.Assist;
import com.macro.mall.mapper.MbrMemberCollectMapper;
import com.macro.mall.model.MbrMemberCollect;
import com.macro.mall.model.MbrMemberHistory;
import com.macro.mall.portal.domain.MemberDetails;
import com.macro.mall.portal.service.MbrMemberCollectService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContext;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * MbrMemberCollect的服务接口的实现类
 *
 * @author gaodx
 */
@Slf4j
@Service
public class MbrMemberCollectServiceImpl implements MbrMemberCollectService {

    @Value("${log.isDebugEnabled:false}")
    private boolean isDebugEnabled;

    @Autowired
    private MbrMemberCollectMapper mbrMemberCollectMapper;

    @Override
    public List<MbrMemberCollect> find(MbrMemberCollect value, Integer pageNum, Integer pageSize) {
        //TODO这里可以做通过Assist做添加查询
        Assist assist = new Assist();
        Long userId = null;
        List<MbrMemberCollect> result = null;
        try {
            SecurityContext ctx = SecurityContextHolder.getContext();
            Authentication auth = ctx.getAuthentication();
            MemberDetails memberDetails = (MemberDetails) auth.getPrincipal();
            userId = memberDetails.getUmsMember().getId();
            assist.andEq("member_id", userId);
            assist.setOrder(Assist.order("create_time", false));
            result = mbrMemberCollectMapper.selectMbrMemberCollect(assist);
        } catch (Exception e) {
            log.error("", e);
            Asserts.fail(ResultCode.FAILED);
        }
        if (isDebugEnabled) {
            log.debug("执行获取MbrMemberCollect数据集-->结果:", result);
        }
        return result;
    }

    @Override
    public List<MbrMemberCollect> findAndVideo(MbrMemberCollect value, Integer pageNum, Integer pageSize) {
        Long userId = null;
        try {
            SecurityContext ctx = SecurityContextHolder.getContext();
            Authentication auth = ctx.getAuthentication();
            MemberDetails memberDetails = (MemberDetails) auth.getPrincipal();
            userId = memberDetails.getUmsMember().getId();
        } catch (Exception e) {
            log.error("", e);
            Asserts.fail(ResultCode.UNAUTHORIZED);
        }
        PageHelper.startPage(pageNum, pageSize);
        return mbrMemberCollectMapper.findAndVideo(userId);
    }

    @Override
    public MbrMemberCollect findOne(Long id) {
        if (id == null) {
            if (isDebugEnabled) {
                log.debug("执行通过MbrMemberCollect的id获得MbrMemberCollect对象-->失败:id不能为空");
            }
            Asserts.fail("执行通过MbrMemberCollect的id获得MbrMemberCollect对象-->失败:id不能为空");
        }
        MbrMemberCollect result = null;
        try {
            result = mbrMemberCollectMapper.selectMbrMemberCollectById(id);
        } catch (Exception e) {
            log.error("", e);
            Asserts.fail(ResultCode.FAILED);
        }
        if (isDebugEnabled) {
            log.debug("执行通过MbrMemberCollect的id获得MbrMemberCollect对象-->结果:", result);
        }
        return result;
    }

    @Override
    public CommonResult saveNotNull(Integer id) {
        MbrMemberCollect mbrMemberCollect = new MbrMemberCollect();
        if (id == null) {
            if (isDebugEnabled) {
                log.debug("执行将MbrMemberCollect中属性值不为null的数据保存到数据库-->失败:对象不能为空");
            }
            Asserts.fail("失败:对象不能为空");
        }
        int result = 0;
        try {
            SecurityContext ctx = SecurityContextHolder.getContext();
            Authentication auth = ctx.getAuthentication();
            MemberDetails memberDetails = (MemberDetails) auth.getPrincipal();
            Long userId = memberDetails.getUmsMember().getId();
            mbrMemberCollect.setMemberId(userId);
            mbrMemberCollect.setCourseId(Long.parseLong(id + ""));
            result = mbrMemberCollectMapper.insertNotNullMbrMemberCollect(mbrMemberCollect);
        } catch (Exception e) {
            log.error("", e);
            Asserts.fail(ResultCode.UNAUTHORIZED);
        }
        if (isDebugEnabled) {
            log.debug("执行将MbrMemberCollect中属性值不为null的数据保存到数据库-->结果:", result);
        }
        return CommonResult.success(null);
    }

    @Override
    public CommonResult updateNotNullById(MbrMemberCollect value) {
        if (value == null) {
            if (isDebugEnabled) {
                log.debug("执行通过MbrMemberCollect的id更新MbrMemberCollect中属性不为null的数据-->失败:对象为null");
            }
            Asserts.fail("执行通过MbrMemberCollect的id更新MbrMemberCollect中属性不为null的数据-->失败:对象为null");
        }
        int result = 0;
        try {
            result = mbrMemberCollectMapper.updateNotNullMbrMemberCollectById(value);
        } catch (Exception e) {
            log.error("", e);
            Asserts.fail(ResultCode.FAILED);
        }
        if (isDebugEnabled) {
            log.debug("执行通过MbrMemberCollect的id更新MbrMemberCollect中属性不为null的数据-->结果:", result);
        }
        return CommonResult.success(null);
    }

    @Override
    public CommonResult deleteById(String id) {
        if (id == null) {
            if (isDebugEnabled) {
                log.debug("执行通过MbrMemberCollect的id删除MbrMemberCollect-->失败:id不能为空");
            }
            Asserts.fail("执行通过MbrMemberCollect的id删除MbrMemberCollect-->失败:id不能为空");
        }
        String[] split = id.split(",");
        mbrMemberCollectMapper.deleteMbrMemberCollectById(split);
        if (isDebugEnabled) {
            //log.debug("执行通过MbrMemberCollect的id删除MbrMemberCollect-->结果:", result);
        }
        return CommonResult.success(null);
    }

    @Override
    public CommonResult deleteByMbrMemberCollect(Integer request) {
        if (request == null) {
            if (isDebugEnabled) {
                log.debug("执行通过MbrMemberCollect的id删除MbrMemberCollect-->失败:id不能为空");
            }
            Asserts.fail("执行通过MbrMemberCollect的id删除MbrMemberCollect-->失败:id不能为空");
        }
        Long userId = null;
        try {
            SecurityContext ctx = SecurityContextHolder.getContext();
            Authentication auth = ctx.getAuthentication();
            MemberDetails memberDetails = (MemberDetails) auth.getPrincipal();
            userId = memberDetails.getUmsMember().getId();
        } catch (Exception e) {
            log.error("", e);
            Asserts.fail(ResultCode.UNAUTHORIZED);
        }
        Assist assist = new Assist(Assist.whereRequire("and course_id = ", request), Assist.whereRequire("and member_id = ", userId));
        int result = mbrMemberCollectMapper.deleteMbrMemberCollectByAssist(assist);
        if (isDebugEnabled) {
            log.debug("执行通过MbrMemberCollect的id删除MbrMemberCollect-->结果:", result);
        }
        return CommonResult.success(null);
    }

    @Override
    public long count(MbrMemberHistory mbrMemberHistory) {
        //TODO这里可以做通过Assist做添加查询
        Assist assist = new Assist();
        Long userId = null;
        Long result = null;
        try {
            SecurityContext ctx = SecurityContextHolder.getContext();
            Authentication auth = ctx.getAuthentication();
            MemberDetails memberDetails = (MemberDetails) auth.getPrincipal();
            userId = memberDetails.getUmsMember().getId();
            assist.andEq("member_id", userId);
            result = mbrMemberCollectMapper.getMbrMemberCollectRowCount(assist);
        } catch (Exception e) {
            log.error("", e);
            Asserts.fail(ResultCode.FAILED);
        }
        if (isDebugEnabled) {
            log.debug("执行获取MbrMemberCollect数据集-->结果:", result);
        }
        return result;
    }

}